package utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TODO
 *
 * @author chenwb25485
 * @version V2.1
 * @since 2021/1/15 16:11
 */
public class ValidatorUtil {
    /**
     * 校验证件号的正则
     * @param certificateNo 证件号
     * @param certificateKind 证件类型
     * @return {@code true}: 匹配<br>{@code false}: 不匹配
     */
    public static boolean checkCertificateNo(String certificateNo,String certificateKind) {
        if (certificateNo ==null) {
            return false;
        }

        if (certificateKind.equals("0")) {
            Pattern p = Pattern.compile(
                "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        if (certificateKind.equals("1")) {
            Pattern p = Pattern.compile("^1[3|4|5|7|8][0-9]{9}$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        if (certificateKind.equals("2")) {
            Pattern p = Pattern.compile("^[HM]\\d{10}$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        if (certificateKind.equals("3")) {
            Pattern p = Pattern.compile("^\\d{8}$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        if (certificateKind.equals("4")) {
            Pattern p = Pattern.compile("^[军]\\d{7}$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        if (certificateKind.equals("5")) {
            Pattern p = Pattern.compile("^[A-Z]\\d{6}[(][0-9A-Z][)]$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        if (certificateKind.equals("6")) {
            Pattern p = Pattern.compile("^[157]\\d{6}[(][0-9][)]$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        //台湾身份证
        if (certificateKind.equals("7")) {
            Pattern p = Pattern.compile("^[A-Z]\\d{9}$");
            Matcher m = p.matcher(certificateNo.trim());
            return m.matches();
        }
        return true;
    }
}
